package com.davy.gridpoint.adapter;

import static android.view.View.INVISIBLE;
import static android.view.View.VISIBLE;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.davy.gridpoint.R;
import com.davy.gridpoint.bean.ChooseBean;
import com.davy.gridpoint.utils.LogUtils;
import com.davy.gridpoint.utils.SPUtils;

import java.util.ArrayList;

public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.ItemViewHolder> {
    private ArrayList<ChooseBean> mList;
    private Context mContext;
    private ItemClickListener mItemClick;

    public HomeAdapter(Context mContext, ArrayList<ChooseBean> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    public void setData(ArrayList<ChooseBean> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(ItemClickListener listener) {
        this.mItemClick = listener;
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemViewHolder itemViewHolder;
        itemViewHolder = new ItemViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_home, parent, false));
        return itemViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
        if (mList.size() > 0) {
            holder.imgDui.setVisibility(INVISIBLE);
            String strSelect = SPUtils.get(mContext, "strSelect", "") + "";
            if (mList.get(position).getName().equals(strSelect)) {
                holder.lyLayout.setBackgroundColor(mContext.getResources().getColor(R.color.bg_2a));
                holder.imgDui.setVisibility(VISIBLE);
            } else {
                holder.lyLayout.setBackgroundColor(mContext.getResources().getColor(R.color.bg_a6));
            }
            holder.tvHome.setText(mList.get(position).getName());
            holder.itemView.setOnClickListener(view -> {
                if (mItemClick != null) {
                    LogUtils.e("TAG", "position:" + position);
                    LogUtils.e("TAG", "size:" + mList.size());
                    mList.get(0).setType(position);
                    mItemClick.ItemClick(position, mList.get(position));
                    notifyDataSetChanged();
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        private TextView tvHome;
        private LinearLayout lyLayout;
        private ImageView imgDui;

        public ItemViewHolder(View itemView) {
            super(itemView);
            tvHome = itemView.findViewById(R.id.tvHome);
            lyLayout = itemView.findViewById(R.id.lyLayout);
            imgDui = itemView.findViewById(R.id.imgDui);
        }
    }

    public interface ItemClickListener {

        void ItemClick(int position, ChooseBean string);
    }
}
